package com.smartfactory.sail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartfactory.sail.dto.GetOrderItemByCustId;
import com.smartfactory.sail.entity.OrderItem;
import com.smartfactory.sail.entity.Plan;
import com.smartfactory.sail.entity.enums.OrderItemStatusEnum;
import com.smartfactory.sail.mapper.OrderItemMapper;
import com.smartfactory.sail.mapper.PlanMapper;
import com.smartfactory.sail.service.IItemWorkingprocedureService;
import com.smartfactory.sail.service.IOrderItemService;
import com.smartfactory.sail.utils.DateUtils;
import com.smartfactory.sail.utils.PageUtils;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.item.indexWpResp;
import com.smartfactory.sail.vo.order.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单产品表 服务实现类
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements IOrderItemService {

    private OrderItemMapper orderItemMapper;
    private IItemWorkingprocedureService iItemWorkingprocedureService;
    private PlanMapper planMapper;

    public OrderItemServiceImpl(OrderItemMapper orderItemMapper, IItemWorkingprocedureService iItemWorkingprocedureService, PlanMapper planMapper) {
        this.orderItemMapper = orderItemMapper;
        this.iItemWorkingprocedureService = iItemWorkingprocedureService;
        this.planMapper = planMapper;
    }

    @Override
    public IPage<ListOrderItemRsp> getOrderItemPage(PageParam pageParam, Map<String, Object> queryParam) {
        Page<ListHTOrderListResp> page = new Page<>();
        PageUtils.createPage(page, pageParam);
        QueryWrapper<ListHTOrderListResp> query = new QueryWrapper<>();
        query.allEq(queryParam);
        IPage<ListOrderItemRsp> orderItemPage = orderItemMapper.getOrderItemPage(page, query);
        //如果是生产中的状态  显示所有工序的进度情况
        for (ListOrderItemRsp record : orderItemPage.getRecords()) {

            LambdaQueryWrapper<Plan> planLambdaQueryWrapper = new LambdaQueryWrapper<>();
            planLambdaQueryWrapper.eq(Plan::getPlanDate, DateUtils.getNowDate());
            planLambdaQueryWrapper.eq(Plan::getOrderId, record.getOrderId());
            planLambdaQueryWrapper.eq(Plan::getItemId, record.getItemId());
            if (planMapper.selectCount(planLambdaQueryWrapper)==0) {
                record.setStatus(OrderItemStatusEnum.JRWPB);
            }

            if (record.getStatus().equals(OrderItemStatusEnum.SCZ)) {
                Long orderId = record.getOrderId();
                Long itemId = record.getItemId();
                record.setPercents(iItemWorkingprocedureService.simple(orderId, itemId, Long.valueOf(queryParam.get("dept").toString())));
            }
        }
        return orderItemPage;
    }

    @Override
    public OrderItem findByOrderIdAndItemId(long itemId, long orderId) {
        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderItem::getItemId, itemId);
        queryWrapper.eq(OrderItem::getOrderId, orderId);
        return orderItemMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean updateStatus(OrderItem orderItem) {
        return orderItemMapper.updateStatus(orderItem);
    }

    @Override
    public List<GetOrderItemByCustId> getOrderItemByCustId(Long custId) {
        return orderItemMapper.getByCustId(custId);
    }

    @Override
    public List<PackingDetailResp> packingDetail(long custId) {
       return orderItemMapper.findPackingDetail(custId);
    }

    @Override
    public IndexOrderItemStatResp indexStat(long deptId) {
        return new IndexOrderItemStatResp(selectStatCount(null, deptId),
                selectStatCount(OrderItemStatusEnum.SCZ.getCode(), deptId),
                selectStatCount(OrderItemStatusEnum.DFP.getCode(), deptId));
    }

    @Override
    public List<PackingDetailResp> packing(long custId) {
        return orderItemMapper.findPackingDetail(custId);
    }

    @Override
    public List<indexWpResp> indexWp(long deptId) {
        return orderItemMapper.indexWp(deptId);
    }

    @Override
    public PackingListResp findOrderItem(long orderId, long itemId) {
        List<PackingDetailResp> packingDetails = orderItemMapper.requestPacking(orderId, itemId);
        if (CollectionUtils.isEmpty(packingDetails))
            return null;
        PackingDetailResp packingDetailResp = packingDetails.get(0);
        PackingListResp packingListResp = new PackingListResp();
        BeanUtils.copyProperties(packingDetailResp, packingListResp);
        packingListResp.setPackingDetailResps(packingDetails);
        return packingListResp;
    }

    private int selectStatCount(Integer code, long deptId) {
        return orderItemMapper.selectStatCount(deptId, code);
    }
}
